﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class ChangeElementsLevel : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Selection selection = commandData.Application.ActiveUIDocument.Selection;
			Document document = commandData.Application.ActiveUIDocument.Document;
			IList<Reference> refElems = null;
			List<Element> allLevels = new FilteredElementCollector(document).OfClass(typeof(Level)).OfCategory(BuiltInCategory.OST_Levels).ToList<Element>();
			Transaction transaction = new Transaction(document);
			transaction.SetName("修改管线");
			try
			{
				for (;;)
				{
					if (selection.GetElementIds().Count != 0)
					{
						FormLevels formLevels = new FormLevels(allLevels);
						while (formLevels.DialogResult == DialogResult.Retry)
						{
							selection.GetElementIds().Clear();
							refElems = selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1, "请选择要修改的管线：");
							formLevels = new FormLevels(allLevels);
						}
						if (formLevels.DialogResult == DialogResult.Cancel)
						{
							break;
						}
						this.SetSelectionSet(refElems, selection, document);
						IEnumerable<ElementId> elementIds = selection.GetElementIds();
						Level level = formLevels.desLevel as Level;
						transaction.Start();
						foreach (ElementId elementId in elementIds)
						{
							Element element = document.GetElement(elementId);
							this.ModifyLevel(element, level);
						}
						RevitVersionFuncs.ClearSelection(selection);
						document.Regenerate();
						transaction.Commit();
					}
					else
					{
						refElems = selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1, "请选择要修改的管线：");
						this.SetSelectionSet(refElems, selection, document);
					}
				}
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
			}
			catch (Exception ex)
			{
				YJKMessageBox.Information(ex.Message);
				if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
				{
					transaction.RollBack();
				}
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void ModifyLevel(Element elem, Level level)
		{
			bool flag = false;
			Document document = elem.Document;
			Parameter parameter = elem.get_Parameter(BuiltInParameter.RBS_START_LEVEL_PARAM);
			if (parameter == null)
			{
				parameter = elem.get_Parameter(BuiltInParameter.FAMILY_LEVEL_PARAM);
				if (parameter == null)
				{
					return;
				}
				flag = true;
			}
			Level level2 = document.GetElement(parameter.AsElementId()) as Level;
			if (!parameter.IsReadOnly && parameter != null)
			{
				parameter.Set(level.Id);
			}
			double num = level2.Elevation - level.Elevation;
			if (flag)
			{
				Parameter parameter2 = elem.GetParameter(BuiltInParameter.RBS_OFFSET_PARAM);
				if (!parameter2.IsReadOnly && parameter2 != null)
				{
					double num2 = parameter2.AsDouble() + num;
					parameter2.Set(num2);
				}
			}
		}

		private void SetSelectionSet(IList<Reference> refElems, Selection sel, Autodesk.Revit.DB.Document doc)
		{
			if (refElems == null)
			{
				return;
			}
			List<Element> list = new List<Element>();
			foreach (Reference reference in refElems)
			{
				list.Add(doc.GetElement(reference));
			}
			RevitVersionFuncs.SetSelection(sel, list);
		}
	}
}
